package com.aiti.lulian.controller.crad;

import com.aiti.base.core.base.controller.WebController;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.entity.CardOperationLog;
import com.aiti.lulian.service.ICardOperationLogService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequiredArgsConstructor
@Api(tags = "名片操作日志控制层")
@RequestMapping("/card/operationLog")
public class CardOperationLogController extends WebController<CardOperationLog> {

    private final ICardOperationLogService cardOperationLogService;

    /**
     * 用于manage-subservice远程调用
     */
    @GetMapping("/updateCardOperationLog")
    public Message updateCardOperationLog(@RequestParam("userId") String userId, @RequestParam("enterpriseId")String enterpriseId) {
        List<CardOperationLog> list1 = cardOperationLogService.list(new LambdaQueryWrapper<CardOperationLog>().eq(CardOperationLog::getUserId, userId));
        if (list1.size() > 0) {
            List<String> cardOperationLogIds1 = list1.stream().map(CardOperationLog::getBaseId).collect(Collectors.toList());
            cardOperationLogService.update(new LambdaUpdateWrapper<CardOperationLog>().in(CardOperationLog::getBaseId, cardOperationLogIds1).set(CardOperationLog::getEnterpriseId, enterpriseId));
        }

        List<CardOperationLog> list2 = cardOperationLogService.list(new LambdaQueryWrapper<CardOperationLog>().eq(CardOperationLog::getVisitorUserId, userId));
        if (list2.size() > 0) {
            List<String> cardOperationLogIds2 = list2.stream().map(CardOperationLog::getBaseId).collect(Collectors.toList());
            cardOperationLogService.update(new LambdaUpdateWrapper<CardOperationLog>().in(CardOperationLog::getBaseId, cardOperationLogIds2).set(CardOperationLog::getVisitorEnterpriseId, enterpriseId));
        }
        return Message.success();
    }
}